<?php 
//通过对象组合水平扩展php类功能

interface CarContract
{
    public function drive();
}

//通过接口类型声明
interface Power
{
    public function power();
}

class Gas implements Power
{
    public function power()
    {   
        # code...
        return "汽油";
    }
}

class Battery implements Power
{
    public function power()
    {
        return "电池";
    }
}

abstract class BaseCar implements CarContract
{
    protected $brand;
    protected $power;

    public function __construct(Power $power, $brand)
    {
        $this->power = $power;
        $this->brand = $brand;
    }

    abstract public function drive();
}



class LynkCo01 extends BaseCar
{
    public function __construct(Power $power, $brand)
    {
        // $this->brand = "领克01";
        parent::__construct($power, $this->brand);
    }

    public function drive()
    {
        echo "启动{$this->brand}汽车" . '<br/>';
        echo "动力来源" . $this->power->power() . '<br/>';
    }
}

//实现与具体的实现类绑定，代码耦合度高，扩展性低，不利于后续运行时自由切换动力来源
// class Gas
// {
//     public function __toString()
//     {
//         return "汽油";
//     }
// }

$gas = new Gas();
$battery = new Battery();

$lynk01 = new LynkCo01($battery,'领克01');
$lynk01->drive();
echo "电力不足，自动切换为使用汽油作为动力来源。。。" . '<br/>';
$lynk01 = new LynkCo01($gas,'领克01');
$lynk01->drive();
// $lynk01->power($gas);

